﻿namespace FallingRocks
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public class RocksFactory : IFactory
    {
        private readonly char[] rocksVisualisation = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-' };

        public IList CreateObjects(int number)
        {
            List<GameObject> rocks = new List<GameObject>();
            Position rockPosition;
            char rockSymbol;
            char[] rockVisualisation;
            Array colors = Enum.GetValues(typeof(Color));
            Color rockColor;

            for (int i = 0; i < RandomNumberGenerator.Instance.Next(1, number); i++)
            {
                rockPosition = new Position(RandomNumberGenerator.Instance.Next(0, Console.WindowWidth), 0);
                rockSymbol = this.rocksVisualisation[RandomNumberGenerator.Instance.Next(this.rocksVisualisation.Length)];
                rockVisualisation = new char[RandomNumberGenerator.Instance.Next(1, number)];

                for (int index = 0; index < rockVisualisation.Length; index++)
                {
                    rockVisualisation[index] = rockSymbol;
                }
                
                rockColor = (Color)colors.GetValue(RandomNumberGenerator.Instance.Next(2, colors.Length));

                Rock rock = new Rock(rockPosition, rockVisualisation, new Position(0, 1), rockColor);
                                
                if (!rocks.Contains(rock) && (rock.Position.X + rock.Visualisation.Length <= Console.WindowWidth - 1))
                {
                    rocks.Add(rock);
                }
            }

            if (RandomNumberGenerator.Instance.Next(100) == 1)
            {
                rocks.Add(new SpecialObject(new Position(RandomNumberGenerator.Instance.Next(0, Console.WindowWidth), 0), new char[] { '?' }, new Position(0, 1), Color.Cyan));
            }

            return rocks;
        }
    }
}
